﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Alert_Form.Component.Alert;
using YXChatFormClient.Dtos.Account;
using YXChatFormClient.Utility;

namespace YXChatFormClient
{
    public partial class FrmRegister : Form
    {
        public FrmRegister()
        {
            InitializeComponent();
            Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, this.Width, this.Height, 15, 15));
        }

        private void picClose_Click_1(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void picClose_MouseMove(object sender, MouseEventArgs e)
        {
            picClose.BackColor = Color.FromArgb(250, 81, 81);
            picClose.ImageLocation = ImageHelper.GetStaticImage("close_active.png");
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            picClose.BackColor = Color.White;
            picClose.ImageLocation = ImageHelper.GetStaticImage("close.png");
        }
        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        private static extern void ReleaseCapture();

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern void SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

        private void panelHearder_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }

        private async void btRegister_Click(object sender, EventArgs e)
        {
            #region 校验
            if (tbNickName.Text.IsNullOrWhiteSpace())
            {
                AlertForm.ShowAlert(@"昵称不能为空",AlertType.Warning);
                return;
            }
            if (tbPhoneNumber.Text.IsNullOrWhiteSpace())
            {
                AlertForm.ShowAlert(@"电话号码不能为空", AlertType.Warning);
                return;
            }
            if (tbAccountPwd.Text.IsNullOrWhiteSpace())
            {
                AlertForm.ShowAlert(@"密码不能为空", AlertType.Warning);
                return;
            }
            if (tbAccountPwd.Text.Length < 6)
            {
                AlertForm.ShowAlert(@"密码不能少于6位数", AlertType.Warning);
                return;
            }
            #endregion

            AccountEditDto registerAccount = new AccountEditDto()
            {
                PhoneNumber = tbPhoneNumber.Text,
                PassWord = tbAccountPwd.Text,
                NickName = tbNickName.Text
            };
            HttpHelper http = new HttpHelper();
            var account =await http.PostAsync<AccountDto>(Api.AccountRegister, registerAccount);
            if (account!=null)
            {
                MessageBox.Show($@"您的Yx号为：{account.AccountNumber}");
            }
        }
    }
}
